
//加载页面上时 执行里面的方法，。
$(function () {
    findProductClassify();
    selectProduct();
})
function findProductClassify() {
    $.ajax({
        data:'',
        dataType:'json',
        type:'get',
        url:'/back/product/findProductClassify',
        success:function (data) {
            data=data.model;
            let html='<option value="">全部类别</option>';
            for (let i=0;i<data.length;i++){
                html +=' <option value="'+data[i].id+'">' + data[i].name + '</option>';
            };
            $("#sortProductClassify").html(html);

        }
    })
}

function selectProduct() {
    let data = {
        pageSize:$("#pageSize").val(),
        pageNumber:$("#pageNumber").val(),
        // sortClassifyName:$("#sortClassifyName").val(),
        sortId:$("#sortId").val(),
        sortPrice:$("#sortPrice").val(),
        sortAverageprice:$("#sortAverageprice").val(),
        sortProductClassify:$("#sortProductClassify").val()
    }
    console.log(data),
    $.ajax({
        url:'/back/product/findProduct',
        data:data,
        dataType:'json',
        type:'get',
        success:function (data) {
            let list = data.list;
            let html='';
            $("#count").html(data.count);
            let count = $("#count").text();
            let pageSize = $("#pageSize").val();
            let a = Math.ceil(count/pageSize);
            $("#pageCount").html(a);
            //页数 改变 上一页下一页颜色=================================================================
            let pageNumber= parseInt($("#pageNumber").val());
            if (pageNumber<=1){
                $("#lastPage").attr('disabled',"true");
                $("#lastPage").css("background-color","silver");
                $("#nextPage").removeAttr("disabled");
                $("#nextPage").removeAttr('style',"");

            }else if (pageNumber==a){
                $("#nextPage").attr('disabled',"true");
                $("#lastPage").removeAttr("disabled");
                $("#nextPage").css("background-color","silver");
                $("#lastPage").removeAttr('style',"");
            }
            for (let i = 0;i<list.length;i++){
                html+='<tr>\n' +
                    '            <td>'+list[i].id+'</td>\n' +
                    '            <td>'+list[i].name+'</td>\n' +
                    '            <td><img class="img" src="/back/img'+list[i].img+'"/></td>\n' +
                    '            <td>'+list[i].content+'</td>\n' +
                    '            <td>'+list[i].diliver+'</td>\n' +
                    '            <td>'+list[i].averageprice+'</td>\n' +
                    '            <td>'+list[i].price+'</td>\n' +
                    '            <td>'+list[i].quality+'</td>\n' +
                    '            <td>'+list[i].payment+'</td>\n' +
                    '            <td>'+list[i].information+'</td>\n' +
                    '            <td>'+list[i].classifyName+'</td>\n' +
                    '            <td><a href="javascript:edit('+list[i].id+')">编辑</a>&nbsp;' +
                    '                 <a href="javascript:del('+list[i].id+')">删除</a>' +
                    '            </td>\n' +
                    '        </tr>';
            }
            $("#findProduct").html(html);
        }
    });

}


//通过id编辑===============================================
function edit(id) {
    localStorage.setItem("productId",id);
    $("#right").load("/back/html/product/productUpdate.html");
}

//通过id删除==============================================
function del(id) {
    $.ajax({
        url:'/back/product/deleteById',
        data:{id:id},
        dataType:'json',
        type:'post',
        success:function (data) {
            if (data.result==1){
                alert("删除成功");
                $('#right').load('/back/html/product/productUsManage.html');
            }else if(data.result==0){
                alert("删除失败");
                $('#right').load('/back/html/product/productManage.html');
            }
        }
    })
}
//模糊查询一些数据
function findProductSome() {
    let data={
        Productname:$("#Productname").val(),
        phone:$("#phone").val(),
        sex:$("#sex").val(),
        firstbirthday:$("#firstbirthday").val(),
        lastbirthday:$("#lastbirthday").val(),
        hobby:hb()
    }
    console.log(data)
    $.ajax({
        url:'/findSome',
        data:data,
        dataType:'json',
        type:'get',
        success:function (data) {
            data = data.list;
            console.log(data);
            let html='';
            for (let i = 0;i<data.length;i++){
                html+='<tr>\n' +
                    '            <td>'+data[i].id+'</td>\n' +
                    '            <td>'+data[i].Productname+'</td>\n' +
                    '            <td>'+data[i].phone+'</td>\n' +
                    '            <td>'+data[i].sex+'</td>\n' +
                    '            <td>'+data[i].birthday+'</td>\n' +
                    '            <td>'+data[i].hobby+'</td>\n' +
                    '            <td>'+data[i].introduce+'</td>\n' +
                    '            <td><a href="javascript:edit('+data[i].id+')">编辑</a>&nbsp;' +
                    '            <a href="javascript:del('+data[i].id+')">删除</a>' +
                    '        </td>\n' +
                    '        </tr>';
            }
            $("#findProduct").html(html);
        }
    })

}
function hb() {
    let hb=[];
    $("input:checkbox[name='hobby']:checked").each(function () {
        let state = ($(this).prop('checked'));
        if (state){
            hb.push($(this).val());
        }
    });
    hb = hb.join(',');
    return hb;

}
function firstPage() {
    $("#pageNumber").val(1);
    $("#nextPage").removeAttr('style',"");
    selectProduct();
}
function lastPage() {
    let a = parseInt($("#pageNumber").val());
    if (a>1){
        $("#pageNumber").val(($("#pageNumber").val()-1));
        $("#nextPage").removeAttr('style',"");
    }
    selectProduct();
}
function nextPage() {
    let a = parseInt($("#pageNumber").val());
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let b = Math.ceil(count/pageSize);
    if (a<b){
        $("#lastPage").removeAttr('style',"");
        $("#pageNumber").val(a+1);
    }
    selectProduct();
}
function changes() {
    let a = parseInt($("#pageNumber").val());
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let b = Math.ceil(count/pageSize);
    if (a<=1){
        $("#pageNumber").val(1);
        $("#lastPage").attr('disabled',"true");
    }else if (a>=b){
        $("#pageNumber").val(b);
        $("#nextPage").attr('disabled',"true");
    }else {
        $("#lastPage").removeAttr("disabled");
    }
}
function endPage() {
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let a = Math.ceil(count/pageSize);
    $("#pageNumber").val(a);
    $("#lastPage").removeAttr('style',"");
    selectProduct();
}

function sortId() {
    $("#sortAverageprice").val("");
    $("#sortPrice").val("");
    let a = $("#sortId").val();
    let b= "desc";
    $("#sortId").val((a.match(b)? "asc":"desc"));
    selectProduct();

}
function sortAverageprice() {
    let a = $("#sortAverageprice").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortPrice").val("");
    $("#sortAverageprice").val((a.match(b)? "desc":"asc"));
    selectProduct();
}
function sortPrice() {
    let a = $("#sortPrice").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortAverageprice").val("");
    $("#sortPrice").val((a.match(b)? "desc":"asc"));
    selectProduct();
}
function sortProductClassify() {
    selectProduct();
}
// function sortClassifyName() {
//     $("#sortId").val("");
//     let a = $("#sortClassifyName").val();
//     let b= "asc";
//     $("#sortClassifyName").val((a.match(b)? "desc":"asc"));
//     selectProduct();
// }